<?php
// 本类由系统自动生成，仅供测试用途
class GimageAction extends Action {
    public function add(){
    	$this->getMuen();
		
		$id = $this->_get('id');
		$c = $this->_get('c');
		
		$this->assign('id', $id);
		$this->assign('c', $c);
		
    	$this->display();
    }
    
    public function addAction(){
    	$this->getMuen();
    	$gid = $this->_get('gid');
    	$c = $this->_get('c');
    	
    	$goods = M('goods');
    	
	    //上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		
		$Img = M('goods_img');
		
		$data = array(
			'gid' => $gid,
			'img' => $info[0]['savename'],
	    	'ctime' => time(),
		);
		
		if($Img->add($data)){
			$goods->where('id ='.$gid)->setInc('img_count');
		    $this->success('添加图片成功', '__APP__/'.$c.'/index');
	    }else{
		    $this->error('添加图片失败');
	    }
		
    }
    
    public function index(){
	    $this->getMuen();
    	$gid = $this->_get('gid');
    	$c = $this->_get('c');
    	
    	$Img = M('goods_img');
    	$data = $Img->where('gid ='.$gid)->select();
    	
    	$this->assign("data", $data);
    	$this->display();
    	
    }
    
    public function delete(){
    	$this->getMuen();
    	
    	$id = $this->_get('id');
    	$gid = $this->_get('gid');
    	
    	$Img = M('goods_img');
    	
    	
	    if($Img->where('id='.$id)->delete()){
			$this->success('删除商品成功', '__URL__/index/gid/'.$gid);
		}
    }
    
    public function edit(){
	  $this->getMuen();
	  $id = $this->_get('id');
	  
	  $Img = M('goods_img');
	  $data = $Img->where('id = '.$id)->find();
	  $this->assign("data", $data);
	  
	  $this->display();
    }
    
    public function editAction(){
    	$this->getMuen();
    	
    	$gid = $this->_get('gid');
    	$id = $this->_get('id');
    	
    	//上传图片
	    import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  './Uploads/';// 设置附件上传目录
		
		$Img = M('goods_img');
		
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
			$data = array(
				'img' => $info[0]['savename'],
	    	);
	    	
			if($Img->where('id = '.$id)->save($data)){
				$this->success('修改图片成功', '__URL__/index/gid/'.$gid);
			}
		}	
    }
    
    public function getMuen(){
    	R("Login/isLogin");
    	
	   	$menu = include(COMMON_PATH.'menu.php');
		$this->assign('menu', $menu);
    }
}